<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Event_db
 *
 * @author francesco
 */
class Event_db {

    /**
     * Questa funzione ritorna il nome della discoteca
     * avendo in ingresso la partita iva.
     *
     * @param <string> $piva
     * @return <string> $discoName
     */
    public static function getNameDsicoByPiva($piva){

        //$piva=$piva['piva'];
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT name FROM disco WHERE piva='" .$piva. "'";
            $discoName=$db->fetchRow($statement);
            if (!$discoName) {
            throw new Exception("Could not find row piva: $piva");
              }
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $discoName;
    }

    /**
     * Questa funzione ritorna il tutti i dati della discoteca
     * avendo in ingresso la partita iva.
     *
     * @param <string> $piva
     * @return <string> $discoName
     */
    public static function getAllDiscoByPiva($piva){

        //$piva=$piva['piva'];

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT * FROM disco WHERE piva='" .$piva. "'";
            $discoName=$db->fetchRow($statement);
            if (!$discoName) {
            throw new Exception("Could not find row $piva");
              }
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $discoName;
    }
    
     /**
     * Questa funzione ritorna il nome del paese
     * in cui si trova un locale avendo in ingresso
     * la partita iva.
     *
     * @param <string> $piva
     * @return <string> $paese
     */
    public function getPaeseDsicoByPiva($piva){

    //$piva=$piva['piva'];
    try{
        require_once "models/Db/Db_Db.php";

        $db=Db_Db::conn();
        $statement="SELECT paese FROM disco WHERE piva='" .$piva. "'";
        $paese=$db->fetchRow($statement);
        if (!$paese) {
        throw new Exception("Could not find row $piva");
          }
        $db->closeConnection();

    } catch(Zend_Db_Exception $e) {
        echo $e->getMessage();
        }
        return $paese = array_unique($paese);
    }

    /**
     *
     * @return <array of string> $pivaList
     */
    public function getAllPiva(){

        //$piva=$piva['piva'];

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT piva FROM disco";
            $pivaList=$db->fetchAll($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $pivaList;
    }

    /**
     *
     * @return <type>
     */
    public function getAllCategory(){

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT * FROM categoria";
            $categoryList=$db->fetchAll($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $categoryList;
    }


    /**
     *
     * @param <int> $id_event the id of the event
     * @return <string> the path of imae
     */
    public function getThumbByID($id_evento){
        //echo "ide: ".$id_evento;
    
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement = "SELECT thumb_path FROM immagine_evento WHERE id_event=".$id_evento;
            $thumb_path = $db->fetchRow($statement);
            //print_r($thumb_path['thumb_path']);
         /*   if (!$thumb_path) {
            throw new Exception("Could not find row $id_evento");
              }*/
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $thumb_path;
    }


    /*
    function specified_array_unique($array)
    {
        $count = 0;
        foreach($array as $array_key => $array_value)
        {

        }
        return $array;
    }
    */
    /**
     * discoActiveEvents() ritorna la piva di tutte i locali
     * con degli eventi attivi.
     *
     * @return <array> piva
     */
    public function discoActiveEvents(){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement = "SELECT piva FROM edisco WHERE status=1";
            $pivaList=$db->fetchAll($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        //return $pivaList = array_unique($pivaList);
        return $pivaList = $pivaList;
    }


    /**
     *
     * @param <type> $data
     * @return <type> 
     */
    public function discoActiveEventsByData($data){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement = 'SELECT piva FROM edisco WHERE status=1 AND data_inizio="'. $data . '"';
            $pivaList=$db->fetchAll($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        //return $pivaList = array_unique($pivaList);
        return $pivaList = $pivaList;
    }


     /**
     * Questa funzione ritorna le piva
     * dei locali residenti in paese
     *
     * @param <string> $paese
     * @return <string> $piva
     */
    public function getPivaDsicoByPaese($paese){

    //$piva=$piva['piva'];
    try{
        require_once "models/Db/Db_Db.php";

        $db=Db_Db::conn();
        $statement="SELECT piva FROM disco WHERE paese='" .$paese. "'";
        $pivaList=$db->fetchAll($statement);

        $db->closeConnection();

    } catch(Zend_Db_Exception $e) {
        echo $e->getMessage();
        }
        return $pivaList;//= array_unique($pivaList);
    }

    /**
     *
     * @param <type> $category
     * @return <type>
     */
    public function getPivaDiscoByCategory($category){

    //$piva=$piva['piva'];
    try{
        require_once "models/Db/Db_Db.php";

        $db=Db_Db::conn();
        $statement="SELECT piva FROM disco WHERE categoria='" .$category. "'";
        $categoryList=$db->fetchAll($statement);
        $db->closeConnection();

    } catch(Zend_Db_Exception $e) {
        echo $e->getMessage();
        }
        return $categoryList;//= array_unique($pivaList);
    }

    public function getPivaDiscoByZona($zona){
    try{
        require_once "models/Db/Db_Db.php";

        $db=Db_Db::conn();
        $statement="SELECT piva FROM disco WHERE provincia='" .$zona. "'";
        $pivaList=$db->fetchAll($statement);
        //$pivaList = $pivaList[0];
        $db->closeConnection();

    } catch(Zend_Db_Exception $e) {
        echo $e->getMessage();
        }
        return $pivaList;//= array_unique($pivaList);
    }

    /**
     * tuttgli gli eventi attivi per una determinata data
     *
     * @param <date> $date
     * @return <event array>
     */
    public function getEventByDate($data){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            //get only active events
            $status=1;
            $statement='SELECT * FROM edisco WHERE status=1 AND data_inizio="'. $data . '"';
            $eventList=$db->fetchAll($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $eventList;
    }

    /**
     * verifico se un locale ha eventi attivi o meno
     *
     * @param <string> $piva
     * @return boolean
     */
    public function exsistActiveEvent($piva){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            //get only active events
            $status=1;
            $statement='SELECT * FROM edisco WHERE status=1 AND piva="'. $piva . '"';
            $eventList=$db->fetchAll($statement);
            if ($eventList!=null){
                $eventList = true;
            } else {
                $eventList = false;
            }
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $eventList;
    }


    /**
     *
     * @param <type> $piva
     * @return <type>
     */
    static public function getUniqueNamebyPiva($piva){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT name,unique_name FROM disco WHERE piva='" .$piva. "'";
            $unique_name=$db->fetchRow($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
            }
            return $unique_name;//= array_unique($pivaList);
        }

        /**
     *
     * @param <type> $piva
     * @return <type>
     */
    static public function getPivaByUniqueName($unique_name){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT piva FROM disco WHERE unique_name='" .$unique_name. "'";
            $piva=$db->fetchRow($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
            }
            return $piva;//= array_unique($pivaList);
        }

    /**
     *
     */
    static public function getCategoriabyPiva($piva){
         try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT categoria FROM disco WHERE piva='" .$piva. "'";
            $categoria=$db->fetchRow($statement);

            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
            }
            return $categoria['categoria'];//= array_unique($pivaList);
    }

}
?>